package com.example.demo_dongruanD.controller;

import com.example.demo_dongruanD.dto.UpdateReportDto;
import com.example.demo_dongruanD.pojo.OverAllResult;
import com.example.demo_dongruanD.response.ApiResponse;
import com.example.demo_dongruanD.service.OverAllResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/result")
public class ReportController {

    private final OverAllResultService service;

    @Autowired
    public ReportController(OverAllResultService service) {
        this.service = service;
    }

    /**
     * 用于进行体检报告的title和content的插入
     * */
    @PostMapping("/reportinsert")
    public ResponseEntity<ApiResponse<OverAllResult>> insertReport(@RequestBody OverAllResult report) {
        OverAllResult savedReport = service.saveOrUpdate(report);
        ApiResponse<OverAllResult> response = new ApiResponse<>(
                HttpStatus.OK.value(),
                "Report inserted successfully",
                savedReport
        );
        return ResponseEntity.ok(response);
    }

    /**
     * Update title and content of a report based on orId.
     */
    @PostMapping("/reportupdate")
    public ResponseEntity<ApiResponse<OverAllResult>> updateReport(@RequestBody UpdateReportDto updateRequest) {
        Integer orId = updateRequest.getOrId();
        String title = updateRequest.getTitle();
        String content = updateRequest.getContent();
        Integer orderId = updateRequest.getOrderId();

        // Retrieve the existing record from repository
        OverAllResult existingReport = service.findById(orId);
        if (existingReport == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
                    new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "Report with ID " + orId + " not found")
            );
        }

        // Update the fields
        existingReport.setTitle(title);
        existingReport.setContent(content);
        existingReport.setOrderId(orderId);

        // Save the updated record
        OverAllResult updatedReport = service.saveOrUpdate(existingReport);

        // Prepare response
        ApiResponse<OverAllResult> response = new ApiResponse<>(
                HttpStatus.OK.value(),
                "Report updated successfully",
                updatedReport
        );
        return ResponseEntity.ok(response);
    }

    /**
     * Delete a report based on orId.
     */
    @PostMapping("/reportdelete")
    public ResponseEntity<ApiResponse<Void>> deleteReport(@RequestBody Map<String, Integer> request) {
        Integer orId = request.get("orId");

        // Check if the report exists
        OverAllResult existingReport = service.findById(orId);
        if (existingReport == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
                    new ApiResponse<>(HttpStatus.NOT_FOUND.value(), "Report with ID " + orId + " not found")
            );
        }

        // Delete the report
        service.delete(orId);

        // Prepare response
        ApiResponse<Void> response = new ApiResponse<>(
                HttpStatus.OK.value(),
                "Report deleted successfully"
        );
        return ResponseEntity.ok(response);
    }
}
